package win.smartown.android.library.richtexteditor;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Smartown on 2017/11/8.
 */
public class RichTextPreview extends ScrollView {

    private RichTextPreviewContent richTextPreviewContent;

    public RichTextPreview(Context context) {
        super(context);
        init();
    }

    public RichTextPreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RichTextPreview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        richTextPreviewContent = new RichTextPreviewContent(getContext());
        addView(richTextPreviewContent);
    }

    public void showContent(List<Content> contentList) {
        richTextPreviewContent.showContent(contentList);
    }

    private static class RichTextPreviewContent extends LinearLayoutCompat {

        private HashMap<String, ImageView> imageViewHashMap;
        private int padding;

        public RichTextPreviewContent(Context context) {
            super(context);
            init();
        }

        private void init() {
            imageViewHashMap = new HashMap<>();
            setOrientation(VERTICAL);
            setGravity(Gravity.CENTER_HORIZONTAL);
            padding = getResources().getDimensionPixelSize(R.dimen.dp16);
            setPadding(padding, 0, padding, 0);
        }

        public void showContent(List<Content> contentList) {
            for (Content content : contentList) {
                addItem(content);
            }
        }

        private void addItem(Content content) {
            switch (content.getType()) {
                case 1:
                    addTextItem(content);
                    break;
                case 2:
                case 3:
                    addImageItem(content);
                    break;
            }
        }

        private void addTextItem(Content content) {
            TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.preview_text, null, true);
            textView.setText(content.getValue());
            addView(textView);
        }

        private void addImageItem(Content content) {
            ImageView imageView = (ImageView) LayoutInflater.from(getContext()).inflate(R.layout.preview_image, null, true);
            imageViewHashMap.put(content.getValue(), imageView);
            Glide.with(getContext()).load(content.getValue()).asBitmap().listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    ImageView imageView = imageViewHashMap.get(model);
                    LinearLayoutCompat.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
                    layoutParams.height = (int) (((float) RichTextPreviewContent.this.getWidth() - 2 * padding) / resource.getWidth() * resource.getHeight());
                    imageView.setLayoutParams(layoutParams);
                    imageViewHashMap.remove(model);
                    return false;
                }
            }).into(imageView);
            addView(imageView);
        }

    }

}
